import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ResData, Status } from 'src/response/res-data.interface';
import { Repository, TreeRepository } from 'typeorm';
import { CreateDeptDto } from './dto/create-dept.dto';
import { UpdateDeptDto } from './dto/update-dept.dto';
import Dept from './entities/dept.entity';

@Injectable()
export class DeptService {
  constructor(@InjectRepository(Dept) private deptRepository: Repository<Dept>, @InjectRepository(Dept) private deptTreeRepository: TreeRepository<Dept>) { }

  async create(createDeptDto: CreateDeptDto) {
    let resdata: ResData;
    try {
      const result = await this.deptRepository.save(createDeptDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '保存成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findAll(querys) {
    let resdata: ResData;
    try {
      const { startTime, endTime, deptName } = querys;
      let sql = this.deptRepository.createQueryBuilder("dept");
      if (deptName) {
        sql.where('dept.deptName like :deptName', { deptName: `%${deptName}%` });
      }
      if (startTime) {
        sql.andWhere('dept.createdDate between :startTime and :endTime', { startTime, endTime })
      }
      const result = await sql.skip(querys.skip).take(querys.take).orderBy("updatedDate", "DESC").getManyAndCount()

      const [list, total] = result
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: {
          result: list,
          total
        }
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findAllTree(querys) {
    let resdata: ResData;
    try {
      const result = await this.deptTreeRepository.findTrees();
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findOne(id: string) {
    let resdata: ResData;
    try {
      const result = await this.deptRepository.findOne(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async update(id: string, updateDeptDto: UpdateDeptDto) {
    let resdata: ResData;
    try {
      const result = await this.deptRepository.update(id, updateDeptDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '更新成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async remove(id: string) {
    let resdata: ResData;
    try {
      const result = await this.deptRepository.delete(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '删除成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }
}
